home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / VGX / shadows / vect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.0 KB  |  232 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * vect -
  19.  *    Various functions to support operations on vectors.
  20.  *
  21.  * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
  22.  *
  23.  * Modified for my own nefarious purposes-- Gavin Bell
  24.  * Further modified by Tim Heidmann, November '91
  25.  */
  26. #include "vect.h"
  27.  
  28. float *
  29. vnew()
  30. {
  31.     register float *v;
  32.  
  33.     v = (float *) malloc(sizeof(float)*3);
  34.     return v;
  35. }
  36.  
  37. float *
  38. vclone(v)
  39. float *v;
  40. {
  41.     register float *c;
  42.  
  43.     c = vnew();
  44.     vcopy(v, c);
  45.     return c;
  46. }
  47.  
  48. void
  49. vcopy(v1,v2)
  50. float *v1, *v2;
  51. {
  52.     register int i;
  53.     for (i = 0 ; i < 3 ; i++)
  54.         v2[i] = v1[i];
  55. }
  56.  
  57. void
  58. vprint(v)
  59. float *v;
  60. {
  61.     printf("x: %f y: %f z: %f\n",v[0],v[1],v[2]);
  62. }
  63.  
  64. void
  65. vset(float *v, float x, float y, float z)
  66. {
  67.     v[0] = x;
  68.     v[1] = y;
  69.     v[2] = z;
  70. }
  71.  
  72. void
  73. vzero(v)
  74. float *v;
  75. {
  76.     v[0] = 0.0;
  77.     v[1] = 0.0;
  78.     v[2] = 0.0;
  79. }
  80.  
  81. void
  82. vnormal(v)
  83. float *v;
  84. {
  85.     vscale(v,1.0/vlength(v));
  86. }
  87.  
  88. float
  89. vlength(v)
  90. float *v;
  91. {
  92.     return fsqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  93. }
  94.  
  95. void
  96. vscale(float *v, float div)
  97. {
  98.     v[0] *= div;
  99.     v[1] *= div;
  100.     v[2] *= div;
  101. }
  102.  
  103. void
  104. vmult(src1,src2,dst)
  105. float *src1, *src2, *dst;
  106. {
  107.     dst[0] = src1[0] * src2[0];
  108.     dst[1] = src1[1] * src2[1];
  109.     dst[2] = src1[2] * src2[2];
  110. }
  111.  
  112. void
  113. vadd(src1,src2,dst)
  114. float *src1, *src2, *dst;
  115. {
  116.     dst[0] = src1[0] + src2[0];
  117.     dst[1] = src1[1] + src2[1];
  118.     dst[2] = src1[2] + src2[2];
  119. }
  120.  
  121. void
  122. vsub(src1,src2,dst)
  123. float *src1, *src2, *dst;
  124. {
  125.     dst[0] = src1[0] - src2[0];
  126.     dst[1] = src1[1] - src2[1];
  127.     dst[2] = src1[2] - src2[2];
  128. }
  129.  
  130. void
  131. vhalf(v1,v2,half)
  132. float *v1, *v2, *half;
  133. {
  134.     float len;
  135.  
  136.     vadd(v2,v1,half);
  137.     len = vlength(half);
  138.     if(len>0.0001)
  139.         vscale(half,1.0/len);
  140.     else
  141.         *half = *v1;
  142. }
  143.  
  144. float
  145. vdot(v1,v2)
  146. float *v1, *v2;
  147. {
  148.     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  149. }
  150.  
  151. void
  152. vcross(v1, v2, cross)
  153. float *v1, *v2, *cross;
  154. {
  155.     float temp[3];
  156.  
  157.     temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  158.     temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  159.     temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  160.     vcopy(temp, cross);
  161. }
  162.  
  163. void
  164. vdirection(v1, dir)
  165. float *v1, *dir;
  166. {
  167.     *dir = *v1;
  168.     vnormal(dir);
  169. }
  170.  
  171. void
  172. vreflect(in,mirror,out)
  173. float *in, *mirror, *out;
  174. {
  175.     float temp[3];
  176.  
  177.     vcopy(mirror, temp);
  178.     vscale(temp,vdot(mirror,in));
  179.     vsub(temp,in,out);
  180.     vadd(temp,out,out);
  181. }
  182.  
  183. void
  184. vmultmatrix(m1,m2,prod)
  185. float m1[4][4], m2[4][4], prod[4][4];
  186. {
  187.     register int row, col;
  188.     float temp[4][4];
  189.  
  190.     for(row=0 ; row<4 ; row++) 
  191.         for(col=0 ; col<4 ; col++)
  192.             temp[row][col] = m1[row][0] * m2[0][col]
  193.                            + m1[row][1] * m2[1][col]
  194.                            + m1[row][2] * m2[2][col]
  195.                            + m1[row][3] * m2[3][col];
  196.     for(row=0 ; row<4 ; row++) 
  197.         for(col=0 ; col<4 ; col++)
  198.         prod[row][col] = temp[row][col];
  199. }
  200.  
  201. void
  202. vtransform(v,mat,vt)
  203. float *v;
  204. float mat[4][4];
  205. float *vt;
  206. {
  207.     float t[3];
  208.  
  209.     t[0] = v[0]*mat[0][0] + v[1]*mat[1][0] + v[2]*mat[2][0] + mat[3][0];
  210.     t[1] = v[0]*mat[0][1] + v[1]*mat[1][1] + v[2]*mat[2][1] + mat[3][1];
  211.     t[2] = v[0]*mat[0][2] + v[1]*mat[1][2] + v[2]*mat[2][2] + mat[3][2];
  212.     vcopy(t, vt);
  213. }
  214.  
  215. void
  216. vmatcopy(float m1[4][4], float m2[4][4]) {
  217.     register int row, col;
  218.     for (row=3; row>=0; row--)
  219.     for (col=3; col>=0; col--)
  220.         m2[row][col] = m1[row][col];
  221. }
  222.  
  223. void
  224. vmattranspose(float m1[4][4], float m2[4][4]) {
  225.     register int row, col;
  226.     float tmp[4][4];
  227.     for (row=3; row>=0; row--)
  228.     for (col=3; col>=0; col--)
  229.         tmp[row][col] = m1[col][row];
  230.     vmatcopy(tmp, m2);
  231. }
  232.